<template>
	<div v-if="key == this.routePath.defaultPath">
		<!-- 搜索 -->
		<el-form :inline="true" :model="searchForm">
			<el-form-item label="模块名称">
				<el-input placeholder="模块名称" v-model="searchForm.moduleName"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
				<el-button class="filter-item" v-if="permission_add" type="primary" icon="el-icon-plus" @click="handleCreate">新增</el-button>
			</el-form-item>
		</el-form>
		<!-- 列表 -->
		<el-table :data="dataList" minheight="calc(100VH - 270px)" highlight-current-row border style="width: 100%" v-loading="loading">
			<el-table-column label="序号" type="index" align="center" width="50"></el-table-column>
			<el-table-column label="模块编号" prop="moduleCode" align="center" ></el-table-column>
			<el-table-column label="模块名称" prop="moduleName" align="center" ></el-table-column>
			<el-table-column label="是否显示" prop="isShow" align="center">
				<template slot-scope="scope">
					<el-switch
					@change="handleStatusChange(scope.$index, scope.row)"
					:active-value="1"
					:inactive-value="2"
					active-color="#13ce66"
					inactive-color="#ff0000"
					v-model="scope.row.isShow">
					</el-switch>
				</template>
			</el-table-column>
			<el-table-column label="创建时间" prop="createTime" align="center" ></el-table-column>
			<el-table-column label="操作" align="center" width="150" fixed="right">
				<template slot-scope="scope">
					<el-button v-if="permission_delete" type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-divider></el-divider>
		<div align="center">
			<el-button type="info" @click="back">返回</el-button>
		</div>
	</div>
	<div v-else>
		<router-view :key="key"></router-view>
	</div>
</template>

<script>
	import { mapGetters } from 'vuex'
	import { findMessageModuleList,deleteMessageModule,modifyMessageModuleShow } from '@/api/platform/message'
	export default {
		name: 'messageModule',
		data() {
			return {
				// 默认加载弹出层
				loading: true,
				//路由地址
				routePath: {
					defaultPath: '/platform/message/modual',
					addPath: '/platform/message/modual/add',
					editPath: '/platform/message/modual/edit',
				},
				// 数据列表el-table
				dataList: [],
				// 权限按钮控制
				permission_add: false,
				permission_edit: false,
				permission_delete: false,
				// 搜索条件
				searchForm: {
					moduleName: '',
				},
			}
		},
		computed: {
			key() {
				return this.$route.path
			},
			...mapGetters([
				'elementList'
			])
		},
		components: {},
		created() {
			if (this.$route.path === this.routePath.defaultPath) {
				// 初始化获取数据列表
				this.getDateList()
				//初始化按钮权限
				this.permission_add = this.elementList['platform:message:modual:add']
				this.permission_edit = this.elementList['platform:message:modual:edit']
				this.permission_delete = this.elementList['platform:message:modual:delete']
			}
		},
		methods: {
			// 方法集合
			getDateList() {
				this.loading = true
				findMessageModuleList(this.searchForm).then(res => {
					this.dataList = res.data.responseData
				})
				this.loading = false
			},
			handleDelete(val) {
				this.$confirm('是否要删除?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					deleteMessageModule(val).then(res => {
						if(res.data.responseCode == 200){
							this.$notify({
								title: '成功',
								message: '删除成功',
								type: 'success',
								duration: 2000,
								onClose: this.getDateList()
							});
						}
					}).catch((err) => {
						this.$message.error(err.data.responseMessage);
					});
				});
			},
			handleCreate() {
				this.$router.push({
					path: this.routePath.addPath
				});
			},
			handleSearch() {
				this.getDateList()
			},
			back(){
				this.$router.go(-1)
			},
			handleStatusChange(index, row){
				this.$confirm('是否要修改该状态?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					let data = {
						moduleId: row.id,
						isShow: row.isShow
					}
					modifyMessageModuleShow(data).then(() => {
						this.$message({
							type: 'success',
							message: '修改成功!'
						})
					}).catch(err => {
						this.$message.error(err.data.responseMessage);
						this.getDateList();
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '取消修改'
					});
					this.getDateList();
				});
			},
		}
	}
</script>
